home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / Kubuntu 8.10 / kubuntu-8.10-desktop-i386.iso / casper / filesystem.squashfs / usr / share / apps / dcopidlng / kalyptusCxxToDcopIDL.pm < prev    next >
Text File  |  2006-03-17  |  6KB  |  214 lines

  1. #***************************************************************************
  2. #            kalyptusCxxToDcopIDL.pm -  Generates idl from dcop headers
  3. #                             -------------------
  4. #    begin                : Fri Jan 25 12:00:00 2000
  5. #    copyright            : (C) 2003 Alexander Kellett
  6. #    email                : lypanov@kde.org
  7. #    author               : Alexander Kellett
  8. #***************************************************************************/
  9.  
  10. #/***************************************************************************
  11. # *                                                                         *
  12. # *   This program is free software; you can redistribute it and/or modify  *
  13. # *   it under the terms of the GNU General Public License as published by  *
  14. # *   the Free Software Foundation; either version 2 of the License, or     *
  15. # *   (at your option) any later version.                                   *
  16. # *                                                                         *
  17. #***************************************************************************/
  18.  
  19. package kalyptusCxxToDcopIDL;
  20.  
  21. use File::Path;
  22. use File::Basename;
  23. use Carp;
  24. use Ast;
  25. use kdocAstUtil;
  26. use kdocUtil;
  27. use Iter;
  28.  
  29. use strict;
  30. no strict "subs";
  31.  
  32. use vars qw/$libname $rootnode $outputdir $opt $debug/;
  33.  
  34. BEGIN
  35. {
  36. }
  37.  
  38. sub writeDoc
  39. {
  40.     ( $libname, $rootnode, $outputdir, $opt ) = @_;
  41.  
  42.     $debug = $main::debuggen;
  43.  
  44.     print STDERR "Preparsing...\n";
  45.  
  46.     # Preparse everything, to prepare some additional data in the classes and methods
  47.     Iter::LocalCompounds( $rootnode, sub { preParseClass( shift ); } );
  48.  
  49.     kdocAstUtil::dumpAst($rootnode) if ($debug);
  50.  
  51.     print STDERR "Writing dcopidl...\n";
  52.  
  53.     print STDOUT "<!DOCTYPE DCOP-IDL><DCOP-IDL>\n";
  54.  
  55.     print STDOUT "<SOURCE>".@{$rootnode->{Sources}}[0]->{astNodeName}."</SOURCE>\n";
  56.  
  57.     print STDOUT map { "<INCLUDE>$_</INCLUDE>\n" } @main::includes_list;
  58.  
  59.     Iter::LocalCompounds( $rootnode, sub { 
  60.         my ($node) = @_;
  61.  
  62.         my ($methodCode) = generateAllMethods( $node );
  63.         my $className = join "::", kdocAstUtil::heritage($node);
  64.  
  65.         if ($node->{DcopExported}) {
  66.         print STDOUT "<CLASS>\n";
  67.         print STDOUT "    <NAME>$className</NAME>\n";
  68.         print STDOUT "    <LINK_SCOPE>$node->{Export}</LINK_SCOPE>\n" if ($node->{Export});
  69.         print STDOUT join("\n", map { "    <SUPER>$_</SUPER>"; } grep { $_ ne "Global"; }
  70.                  map {
  71.                 my $name = $_->{astNodeName};
  72.                 $name =~ s/</</;
  73.                 $name =~ s/>/>/;
  74.                 my $tmpl = $_->{TmplType};
  75.                 $tmpl =~ s/</</;
  76.                 $tmpl =~ s/>/>/;
  77.                 $tmpl ? "$name<<TYPE>$tmpl</TYPE>>" : $name;
  78.                  } @{$node->{InList}}) . "\n";
  79.         print STDOUT $methodCode;
  80.  
  81.         print STDOUT "</CLASS>\n";
  82.         }
  83.     });
  84.  
  85.     print STDOUT "</DCOP-IDL>\n";
  86.     
  87.     print STDERR "Done.\n";
  88. }
  89.  
  90. =head2 preParseClass
  91.     Called for each class
  92. =cut
  93. sub preParseClass
  94. {
  95.     my( $classNode ) = @_;
  96.     my $className = join( "::", kdocAstUtil::heritage($classNode) );
  97.  
  98.     if( $#{$classNode->{Kids}} < 0 ||
  99.         $classNode->{Access} eq "private" ||
  100.         $classNode->{Access} eq "protected" || # e.g. QPixmap::QPixmapData
  101.         exists $classNode->{Tmpl} ||
  102.         $classNode->{NodeType} eq 'union' # Skip unions for now, e.g. QPDevCmdParam
  103.       ) {
  104.         print STDERR "Skipping $className\n" if ($debug);
  105.         print STDERR "Skipping union $className\n" if ( $classNode->{NodeType} eq 'union');
  106.         delete $classNode->{Compound}; # Cheat, to get it excluded from Iter::LocalCompounds
  107.         return;
  108.     }
  109. }
  110.  
  111.  
  112. sub generateMethod($$)
  113. {
  114.     my( $classNode, $m ) = @_;    # input
  115.     my $methodCode = '';    # output
  116.  
  117.     my $name = $m->{astNodeName}; # method name
  118.     my @heritage = kdocAstUtil::heritage($classNode);
  119.     my $className  = join( "::", @heritage );
  120.  
  121.     # Check some method flags: constructor, destructor etc.
  122.     my $flags = $m->{Flags};
  123.  
  124.     if ( !defined $flags ) {
  125.     warn "Method ".$name.  " has no flags\n";
  126.     }
  127.  
  128.     my $returnType = $m->{ReturnType};
  129.     $returnType = undef if ($returnType eq 'void');
  130.  
  131.     # Don't use $className here, it's never the fully qualified (A::B) name for a ctor.
  132.     my $isConstructor = ($name eq $classNode->{astNodeName} );
  133.     my $isDestructor = ($returnType eq '~');
  134.  
  135.     if ($debug) {
  136.         print STDERR " Method $name";
  137.     print STDERR ", is DTOR" if $isDestructor;
  138.     print STDERR ", returns $returnType" if $returnType;
  139.     #print STDERR " ($m->{Access})";
  140.     print STDERR "\n";
  141.     }
  142.  
  143.     # Don't generate anything for destructors
  144.     return if $isDestructor;
  145.  
  146.     my $args = "";
  147.  
  148.     foreach my $arg ( @{$m->{ParamList}} ) {
  149.  
  150.     print STDERR "  Param ".$arg->{astNodeName}." type: ".$arg->{ArgType}." name:".$arg->{ArgName}." default: ".$arg->{DefaultValue}."\n" if ($debug);
  151.  
  152.     my $argType = $arg->{ArgType};
  153.  
  154.     my $x_isConst = ($argType =~ s/const//);
  155.     my $x_isRef = ($argType =~ s/&//);
  156.  
  157.     my $typeAttrs = "";
  158.     $typeAttrs .= "  qleft=\"const\"" if $x_isConst;
  159.     $typeAttrs .= " qright=\"&\"" if $x_isRef;
  160.  
  161.     $argType =~ s/^\s*(.*?)\s*$/$1/;
  162.     $argType =~ s/</</g;
  163.     $argType =~ s/>/>/g;
  164.     $argType =~ s/\s//g;
  165.  
  166.     $args .= "        <ARG><TYPE$typeAttrs>$argType</TYPE><NAME>$arg->{ArgName}</NAME></ARG>\n";
  167.     }
  168.  
  169.     my $qual = "";
  170.     $qual .= " qual=\"const\"" if $flags =~ "c";
  171.  
  172.     $returnType = "void" unless $returnType;
  173.     $returnType =~ s/</</g;
  174.     $returnType =~ s/>/>/g;
  175.     $returnType =~ s/^\s*const\s*//;
  176.  
  177.     my $methodCode = "";
  178.  
  179.     my $tagType = ($flags !~ /z/) ? "FUNC" : "SIGNAL";
  180.     my $tagAttr = "";
  181.     $tagAttr .= " hidden=\"yes\"" if $flags =~ /y/;
  182.  
  183.     if (!$isConstructor) {
  184.     $methodCode .= "    <$tagType$tagAttr$qual>\n";
  185.     $methodCode .= "        <TYPE>$returnType</TYPE>\n";
  186.     $methodCode .= "        <NAME>$name</NAME>\n";
  187.     $methodCode .= "$args";
  188.     $methodCode .= "     </$tagType>\n";
  189.     }
  190.  
  191.     return ( $methodCode );
  192. }
  193.  
  194. sub generateAllMethods
  195. {
  196.     my ($classNode) = @_;
  197.     my $methodCode = '';
  198.  
  199.     # Then all methods
  200.     Iter::MembersByType ( $classNode, undef,
  201.               sub {    my ($classNode, $methodNode ) = @_;
  202.  
  203.         if ( $methodNode->{NodeType} eq 'method' ) {
  204.         next unless $methodNode->{Flags} =~ /(d|z|y)/;
  205.         my ($meth) = generateMethod( $classNode, $methodNode );
  206.         $methodCode .= $meth;
  207.     }
  208.                   }, undef );
  209.  
  210.     return ( $methodCode );
  211. }
  212.  
  213. 1;
  214.